package com.ruoyi.demo.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.lang.TypeReference;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.demo.domain.TestDemo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * <简述>
 * <详细描述>
 *
 * @author syf
 * @date 2024年08月11日 15:18
 */
@RestController
@Slf4j
@SaIgnore
public class TestSpringUtilsController {

    @GetMapping("test1")
    public void  test(){
        TestSpringUtilsController bean = SpringUtils.getBean(TestSpringUtilsController.class);
        bean.test1();
    }


    public void test1(){
        log.info("test1被调用");
    }


    @GetMapping("test2")
    public void  test2(){
        Map<String, TestDemo> beansOfType = SpringUtils.getBeansOfType(TestDemo.class);

        for (String s :beansOfType.keySet()){
            log.info("key:{}",s);
            log.info("bean:{}",beansOfType.get(s));
        }
    }

    @GetMapping("test3")
    public void  test3(){
        TestDemo testDemo = new TestDemo();
        testDemo.setId(1L);
        //注册
        SpringUtils.registerBean("testDemo", testDemo);
        Object bean1 = SpringUtils.getBean("testDemo");
        log.info("testDemo:{}",bean1);
        //注销
        SpringUtils.unregisterBean("testDemo");
        try {
            Object bean2 = SpringUtils.getBean("testDemo");
            log.info("testDemo:{}",bean2);
        }catch (Exception e){
            log.error("未获取到bean");
        }
    }

    @Bean("diyName")
    public TestDemo testDemoFun(){
        return new TestDemo();
    }

    @Bean("diyName1")
    public TestDemo testDemoFun1(){
        return new TestDemo();
    }

    @GetMapping("test4")
    public void  test4(){
        try {

        String[] beanNamesForType = SpringUtils.getBeanNamesForType(TestDemo.class);
        log.info("beanNamesForType:{}",beanNamesForType);

        //是否包含bean
        boolean testDemo = SpringUtils.containsBean("diyName");
        log.info("containsBean:{}",testDemo);

        //是否单例
        boolean isSingleton = SpringUtils.isSingleton("diyName1");
        log.info("isSingleton:{}",testDemo);

        //获取应用名称
        String applicationName = SpringUtils.getApplicationName();
        log.info("applicationName:{}",applicationName);
        }catch (Exception e){
            log.error("未获取到bean");
        }
    }

    @GetMapping("test5")
    public void  test5(){
        try {
            String property = SpringUtils.getProperty("server.port");
            log.info("property:{}",property);
        }catch (Exception e){
            log.error("未获取到bean");
        }
    }


   /* @GetMapping("test6")
    public void  test6(){
        //String s = test7();
        String s = SpringUtils.getAopProxy(this).test7();
        log.info("test7:{}",s);
    }


    @Cacheable(value = "test7",key = "'key'")
    public String test7(){
        log.info("直接进入test7方法");
        return "ss";
    }*/

    @Bean("myMap")
    public Map<String, Object> test8(){
        Map<String, Object> map = new HashMap<>();
        map.put("test8","test8Value");
        return map;
    }

    @GetMapping("test9")
    public void  test9(){
        Map<String, Object> myMap = SpringUtils.getBean("myMap");
        log.info("myMap:{}",myMap);

        //根据bean类型获取
        Map<String, Object> en = SpringUtils.getBean(new TypeReference<Map<String, Object>>() {});
        log.info("bean:{}",myMap);
    }









}
